﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Web.Mvc.Paging;

namespace Kooboo.Web.Areas.Commerce.Controllers
{
    using Kooboo.Commerce;
    using Kooboo.IoC;
    using Kooboo.Web.Mvc;
    using Kooboo.Commerce.Contexts.Products;

      
    public class ProductQuestionController : ManagementController
    {
        [HttpGet]
        public ActionResult Index(int? page,int? pageSize)
        {
            var questions = this.Actor
                .QueryProductQuetions()
                .OrderByDescending(i => i.Id);

            ViewData["Pager"] = questions.BuildPagerModel(this.Url.Action<ProductQuestionController>(i=>i.Index(null,null)), page ?? 1, pageSize ?? Configuration.PageSize);
            ViewData["delete_action"] = this.Url.Action<ProductQuestionController>(i => i.Delete(null));
            ViewData["answer_action"] = this.Url.Action<ProductQuestionController>(i => i.Answer(UrlParameter<int>.Empty, null));
               
          
            var skipCount = ((page ?? 1) - 1) * (pageSize ?? Kooboo.Commerce.Configuration.PageSize);
            var takeCount = pageSize ?? Kooboo.Commerce.Configuration.PageSize;

            var productUrl = this.Url.Action<ProductController>(i => i.Edit(-1));

            ViewData["questions"] = questions
                .Skip(skipCount)
                .Take(takeCount)
                .Select(i => new
                {
                    i.Id,
                    i.Question,
                    QuestionDate = i.QuestionDate==null?"":i.QuestionDate.Value.ToString(),
                    i.QuestionBy,
                    i.QuestionEmail,
                    i.Answer,
                    AnswerDate = i.AnswerDate==null?"":i.AnswerDate.Value.ToString(),
                    i.AnsweredBy,
                    ProductId = i.Product.Id,
                    ProductName = i.Product.Name,
                    ProductUrl = productUrl.Replace("-1",i.Product.Id.ToString())
                })
                .ToArray()
                .SerializeToJson();

            return View();
        }

      

        [HttpPost]
        public ActionResult Answer(int questionId, string answer)
        {
            if (String.IsNullOrEmpty(answer))
            {
                return Json(new { Message = "Please enter answer." });
            }

            if (answer.Length > 500)
            {
                return Json(new { Message = "Invalid question answer." });
            }

            if (ModelState.IsValid)
            {
                var question = this.Actor.QueryProductQuetions()
                    .Where(i => i.Id == questionId)
                    .FirstOrDefault();

                question.AnsweredBy = ContextContainer.Current.Resolve<Kooboo.Connect.User>().Name;
                question.AnswerDate = DateTime.Now;
                question.Answer = answer;

                this.Actor.Put(question);

                return Json(new
                {
                    Data = new
                    {
                        AnswerDate = question.AnswerDate.ToString(),
                        question.AnsweredBy,
                        question.Answer
                    }
                });

            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }


        [HttpPost]
        public ActionResult Delete(IProductQuestion productQuestion)
        {
            this.Actor.Delete(productQuestion);


            return Json(true);
        }

     
    }
}
